﻿//计件工资
var columns =
            [
                 { display: 'Key', name: 'Id', type: 'text', hide: 1 },
                  { display: 'Key', name: 'WagesBillID', type: 'text', hide: 1 },
                 { display: 'IsHeadman', name: 'IsHeadman', type: 'text', hide: 1 },
                 { display: 'BillState', name: 'BillState', type: 'text', hide: 1 },
                  { display: 'WorkGroupID', name: 'WorkGroupID', type: 'text', hide: 1 },
                 { display: '工作组名称', name: 'RefeName', mintWidth: 80, width: 120, type: 'text' },
                 { display: '员工类别', name: 'IsHeadmanName', mintWidth: 80, width: 120, type: 'text' },
                 { display: '员工名称', name: 'Name', mintWidth: 80, width: 120, type: 'text' },
                 { display: '计件工资', name: 'PieceWages', mintWidth: 60, width: 80, type: 'float' },
                 { display: '扣废品费', name: 'ScrapMoney', mintWidth: 60, width: 80, type: 'float'},
                 { display: '组长提成', name: 'HeadmanAward', mintWidth: 60, width: 80, type: 'float' },
                 { display: '生活费', name: 'LiftMoney', mintWidth: 60, width: 80, type: 'float', editor: { type: 'float'} },
                 { display: '扣其他费', name: 'OtherMoney', mintWidth: 60, width: 80, type: 'float', editor: { type: 'float'} },
                 { display: '补贴药费', name: 'SubsidyMoney', mintWidth: 60, width: 80, type: 'float' },
                 { display: '实发工资', name: 'Wages', mintWidth: 60, width: 80, type: 'float' }//,
//{ display: '单子状态', name: 'BillStateName', mintWidth: 60, width: 80, type: 'text' }
// { display: '是否结算', name: 'IsSettleName', mintWidth: 60, width: 80, type: 'text' }
             ];

var manager;
//init controls
$(document).ready(function () {
    editLoadData();
    $("#okBtn").unbind("click").bind("click", function () { initGridData(); }); //确定(填写数据，确认)
});
function editLoadData() {
    var id = $("#Id").val();
    if (id != undefined && id != "" && id != $("#hidEmpty").val()) {
        initligerGrid(false);
    }
}
//确定导入数据
function initGridData() {
    if ($("#BillState").val() == "1") { $.ligerDialog.success("已提交，不能再更新!");return; }
    initligerGrid(true);
}
function initligerGrid(isInit) {
    manager = null;
    manager = $("#workGroupUserWageGrid").ligerGrid({
        columns: columns,
        //  title: '销售单明细',//不能要标题，修改的BUG，不在ligerui.min.js文件中，所以这个BUG还是存在
        showTitle: false,
        width: '100%',
        enabledSort: true,
        enabledEdit: true,
        // height: '80%',
        checkbox: false,
        rownumbers: true,
//        toolbar: { items: [
//            // { text: '销售单明细' },
//                                    {text: '增加', click: addNewRow, icon: 'add' },
//                                    { line: true },
//            //                                    { text: '修改', click: modifyOperation, icon: 'modify' },
////            //                                    { line: true },
//                                    {text: '删除', click: deleteOperation, img: '../Content/images/delete.gif' }
//                                    ]
//        },
        //        isChecked: f_isChecked, onCheckRow: f_onCheckRow, onCheckAllRow: f_onCheckAllRow,
        onAfterEdit: f_onAfterEdit,
        parms: { id: $("#Id").val(), workGroup: $("#WorkGroupID").val(), StartDate: $("#StartDate").val(), EndDate: $("#EndDate").val(), ScrapMoney: $("#SumScrapMoney").val(), HeadmanAward: $("#HeadmanAward").val(), SubsidyMoney: $("#SumSubsidyMoney").val() },
        url: "../Wages/GetJsonOfWagesBills?isInit=" + isInit
    });
}
function f_onAfterEdit(e) {
    if (manager != null && manager != undefined) {
        manager.updateCell('Wages', e.record.PieceWages + e.record.ScrapMoney + e.record.HeadmanAward + e.record.LiftMoney + e.record.OtherMoney + e.record.SubsidyMoney, e.record);
    }
}
////delete
//function deleteOperation() {

//    var row = manager.getSelectedRow();
//    var ids = checkedCustomer.join(',');
//    var billId = $("#Id").val();
//    var detailId = (row == undefined || row == null) ? null : row["ID"];
//    var confirmState = $("#BillState").val();
//    if (row != null && row != undefined && detailId != null && $.trim(detailId.toString()).length > 0) {
//        ids = detailId.toString() + ",";
//        $.ligerDialog.confirm('确定要删除吗？', function (yes) {
//            if (yes) {
//                if (confirmState == 0) {
//                    manager.deleteSelectedRow();
//                    if ($("#HidEmptyGuid").val() + "," != ids) {//删除非新增的
//                        $.get("../ProductOut/Delete?billId=" + billId + "&ids=" + ids, {}, function (data) {
//                            if (data != undefined && data == "9") {
//                                initligerGrid();
//                            }
//                        });
//                    }
//                }
//                else {
//                    $.ligerDialog.success('已提交，不能删除！'); return;
//                }

//            }
//        });
//    }
//    else {
//        $.ligerDialog.success('请选择需要删除行！'); return;
//    }
//}


////提交
//function confirmOperation() {
//    var billId = $("#ID").val();
//    if (billId != null && billId != undefined && $.trim(billId).length > 0) {
//        $.ligerDialog.confirm('确定要提交吗？', function (yes) {
//            if (yes) {
//                if ($("#BillState").val() != "1") {
//                    $.get("../ProductOut/Confirm?billId=" + billId, {}, function (data) {
//                        if (data != undefined && data == "9") {
//                            $.ligerDialog.success('提交成功!');
//                            //initligerGrid();
//                            ajaxSubmitSuccess(); //公共类：提交成功后关闭当前窗口
//                        }
//                        else if (data == "0") {
//                            $.ligerDialog.success('请先保存后再提交!');
//                        }
//                        else {
//                            $.ligerDialog.success('系统异常!');
//                        }
//                    });
//                } else {
//                    $.ligerDialog.success('已提交，不能重复提交!');
//                }
//            }
//        });
//    }
//    else {
//        $.ligerDialog.success('请先保存后再提交!');
//    }
//}


function f_onCheckAllRow(checked) {
    for (var rowid in this.records) {
        if (checked)
            addCheckedCustomer(this.records[rowid]['CustomerID']);
        else
            removeCheckedCustomer(this.records[rowid]['CustomerID']);
    }
}

/*
该例子实现 表单分页多选
即利用onCheckRow将选中的行记忆下来，并利用isChecked将记忆下来的行初始化选中
*/

var checkedCustomer = [];
function findCheckedCustomer(CustomerID) {
    for (var i = 0; i < checkedCustomer.length; i++) {
        if (checkedCustomer[i] == CustomerID) return i;
    }
    return -1;
}
function addCheckedCustomer(CustomerID) {
    if (findCheckedCustomer(CustomerID) == -1)
        checkedCustomer.push(CustomerID);
}
function removeCheckedCustomer(CustomerID) {
    var i = findCheckedCustomer(CustomerID);
    if (i == -1) return;
    checkedCustomer.splice(i, 1);
}
function f_isChecked(rowdata) {
    if (findCheckedCustomer(rowdata.CustomerID) == -1)
        return false;
    return true;
}
function f_onCheckRow(checked, data) {
    if (checked) addCheckedCustomer(data.ID);
    else removeCheckedCustomer(data.ID);
}
function f_getChecked() {
    alert(checkedCustomer.join(','));
}
